<!DOCTYPE html>
<html>

<head>
  <script src="../node_modules/angular/angular.js"></script>
</head>

<body ng-app="demo">
  <div ng-controller="DemoController">
    <button ng-click="queryResult()">测试</button>
  </div>
  <script>
    const demo = angular.module('demo', [])
    demo.controller('DemoController', function ($scope) {
      $scope.queryResult = function () {
        createRequestPromise('./result.txt').then(res => {
          console.log(res)
        })
      }
    });

    function createRequest(url, successCallback, errorCallback) {
      const xhr = new XMLHttpRequest();
      xhr.open("GET", url, true);
      xhr.onload = (e) => {
        if (xhr.readyState === 4) {
          if (xhr.status === 200) {
            successCallback(xhr.responseText)
          } else {
            errorCallback(xhr.statusText);
          }
        }
      };
      xhr.onerror = (e) => {
        errorCallback(xhr.statusText);
      };
      xhr.send(null);
    }

    function createRequestPromise(url) {
      return new Promise((resolve, reject) => {
        // createRequest(resolve, reject)
        const xhr = new XMLHttpRequest();
        xhr.open("GET", url, true);
        xhr.onload = (e) => {
          if (xhr.readyState === 4) {
            if (xhr.status === 200) {
              resolve(xhr.responseText)
            } else {
              reject(xhr.statusText);
            }
          }
        };
        xhr.onerror = (e) => {
          reject(xhr.statusText);
        };
        xhr.send(null);
      })
    }

  </script>
</body>

</html>